#include "XmlParser.h"
#include "tinyxml.h"

#include <vector>
#include "Triangle.h"
#include "Rectangle.h"
#include "Polygone.h"

// Constructor(string)
XmlParser::XmlParser(const std::string _FileName)
{
	TiXmlDocument file( _FileName.c_str() ) ;
	if(!file.LoadFile())
	{
		exit(-1) ;
	}
	else
	{
		TiXmlHandle* Racine = new TiXmlHandle(&file) ;
		
		// iterate over Triangle
		TiXmlNode * pChild ;
		for ( pChild = Racine->FirstChild("Triangle").ToNode() ; pChild != NULL ; pChild = pChild->NextSibling("Triangle"))
		{
			std::vector<std::pair<float, float> > corners ;
			// iterate over Point
			TiXmlNode * pPoint ;
			for(pPoint = pChild->FirstChild("Point") ; pPoint != NULL ; pPoint = pPoint->NextSibling("Point"))
			{
				float x, y ;
				if(pPoint->ToElement()->QueryFloatAttribute("x", &x) != TIXML_SUCCESS) exit(-2) ;
				if(pPoint->ToElement()->QueryFloatAttribute("y", &y) != TIXML_SUCCESS) exit(-2) ;
				corners.push_back(std::pair<float,float>(x, y)) ;
			}

			// find the color or put it to black if not found
			sf::Color color(0, 0, 0, 255) ;
			if(pChild->FirstChild("Color") != NULL)
			{
				int r, g, b ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("r", &r) != TIXML_SUCCESS) exit(-2) ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("g", &g) != TIXML_SUCCESS) exit(-2) ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("b", &b) != TIXML_SUCCESS) exit(-2) ;
				color.r = r ;
				color.g = g ;
				color.b = b ;
			}
			m_Drawables.push_back(new sffs::Triangle(corners[0], corners[1], corners[2], color)) ;
		}

		// iterate over Rectangle
		for ( pChild = Racine->FirstChild("Rectangle").ToNode() ; pChild != NULL ; pChild = pChild->NextSibling("Rectangle"))
		{
			std::pair<float, float> center, wh ;
			
			// find Center
			if(pChild->FirstChild("Center")->ToElement()->QueryFloatAttribute("x", &center.first) != TIXML_SUCCESS) exit(-2) ;
			if(pChild->FirstChild("Center")->ToElement()->QueryFloatAttribute("y", &center.second) != TIXML_SUCCESS) exit(-2) ;
			
			// find width and height
			if(pChild->FirstChild("Dimension")->ToElement()->QueryFloatAttribute("w", &wh.first) != TIXML_SUCCESS) exit(-2) ;
			if(pChild->FirstChild("Dimension")->ToElement()->QueryFloatAttribute("h", &wh.second) != TIXML_SUCCESS) exit(-2) ;

			// find the color or put it to black if not found
			sf::Color color(0, 0, 0, 255) ;
			if(pChild->FirstChild("Color") != NULL)
			{
				int r, g, b ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("r", &r) != TIXML_SUCCESS) exit(-2) ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("g", &g) != TIXML_SUCCESS) exit(-2) ;
				if(pChild->FirstChild("Color")->ToElement()->QueryIntAttribute("b", &b) != TIXML_SUCCESS) exit(-2) ;
				color.r = r ;
				color.g = g ;
				color.b = b ;
			}
			m_Drawables.push_back(new sffs::Rectangle(center, wh, color)) ;
		}

		// TODO QUESTION3
		// iterate over polygone
		for ( pChild = Racine->FirstChild("Polygone").ToNode() ; pChild != NULL ; pChild = pChild->NextSibling("Polygone"))
		{
			std::vector<std::pair<float, float> > corners ;
			// iterate over Point
			TiXmlNode * pPoint ;
			for(pPoint = pChild->FirstChild("Point") ; pPoint != NULL ; pPoint = pPoint->NextSibling("Point"))
			{
				float x, y ;
				if(pPoint->ToElement()->QueryFloatAttribute("x", &x) != TIXML_SUCCESS) exit(-2) ;
				if(pPoint->ToElement()->QueryFloatAttribute("y", &y) != TIXML_SUCCESS) exit(-2) ;
				corners.push_back(std::pair<float,float>(x, y)) ;
			}

			// find the color
			TiXmlNode * pColor ;
			std::vector<sf::Color> colorVector ;
			for(pColor = pChild->FirstChild("Color") ; pColor != NULL ; pColor = pColor->NextSibling("Color"))
			{
				int r, g, b ;
				if(pColor->ToElement()->QueryIntAttribute("r", &r) != TIXML_SUCCESS) exit(-2) ;
				if(pColor->ToElement()->QueryIntAttribute("g", &g) != TIXML_SUCCESS) exit(-2) ;
				if(pColor->ToElement()->QueryIntAttribute("b", &b) != TIXML_SUCCESS) exit(-2) ;
				colorVector.push_back(sf::Color(r,g,b)) ;
			}
			m_Drawables.push_back(new sffs::Polygone(corners, colorVector)) ;
		}
	}
}

// Destructor
XmlParser::~XmlParser()
{
}